package org.chen.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.chen.domain.entity.VideoAvatar;

import java.util.List;

@Mapper
public interface AvatarMapper extends BaseMapper<VideoAvatar> {
    
    /**
     * 获取所有虚拟人物ID
     */
    @Select("SELECT avatar_id FROM video_avatar")
    List<String> selectAllAvatarIds();
    
    /**
     * 批量插入
     */
    int batchInsert(@Param("list") List<VideoAvatar> list);
    
    /**
     * 批量更新
     */
    int batchUpdate(@Param("list") List<VideoAvatar> list);

    default boolean saveOrUpdate(VideoAvatar entity) {
        VideoAvatar exist = selectOne(
                new QueryWrapper<VideoAvatar>()
                        .eq("avatar_id", entity.getAvatarId())
        );

        if (exist != null) {
            entity.setId(exist.getId());
            return updateById(entity) > 0;
        } else {
            return insert(entity) > 0;
        }
    }
}